﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using DevExpress.XtraEditors.DXErrorProvider;
using QLKDS.DTO;


namespace QLKDS.Forms
{
    public partial class frmEmployeeEdit : XtraForm
    {
        #region Properties
        
        private EmployeeManager employeeManager;
        private NationalManager nationalManager=new NationalManager();
        private ReligiousManager religiousManager = new ReligiousManager();
        private DepartmentalManager departmentalManager = new DepartmentalManager();
        public EmployeeInfo Employee;
        #endregion

        #region Methods

        private void SetMaxLengthOfTextBoxes()
        {
            this.txtFullName.Properties.MaxLength = 32;
            this.txtCode.Properties.MaxLength = 8;
            this.txtMobile.Properties.MaxLength = 16;
           
        }

        /// <summary>
        /// Clear all Textboxes on this form
        /// </summary>
        private void ClearTextBoxes()
        {
            this.txtCode.Text = String.Empty;
            this.txtFullName.Text = String.Empty;
            this.txtIdentityCard.Text = String.Empty;
            this.txtMobile.Text = String.Empty;
            this.txtAddress.Text = String.Empty;
            this.txtEmail.Text = String.Empty;
            this.txtDescription.Text = String.Empty;
            this.dteBirthday.DateTime = DateTime.Now;
            this.txtFullName.Focus();
        }

        private void LoadNational()
        {
            this.lueNational.Properties.DataSource = this.nationalManager.ListNational();
            this.lueNational.Properties.ValueMember = "Id";
            this.lueNational.Properties.DisplayMember = "Name";
        }

        private void LoadDepartmental()
        {
            this.lueDepartmental.Properties.DataSource = this.departmentalManager.ListDepartmental();
            this.lueDepartmental.Properties.ValueMember = "Id";
            this.lueDepartmental.Properties.DisplayMember = "Name";
        }

        private void LoadReligious()
        {
            this.lueReligious.Properties.DataSource = this.religiousManager.ListReligious();
            this.lueReligious.Properties.ValueMember = "Id";
            this.lueReligious.Properties.DisplayMember = "Name";
        }

        private void LoadData()
        {
            this.txtCode.Text = Employee.Code;
            this.txtFullName.Text = Employee.Fullname;
            this.txtIdentityCard.Text = Employee.IdentityCard;
            this.txtMobile.Text = Employee.Mobile;
            this.cbSex.Text = Employee.Sex;
            this.txtAddress.Text = Employee.Address;
            this.txtEmail.Text = Employee.Email;
            this.txtDescription.Text = Employee.Description;
            this.dteBirthday.DateTime = Employee.Birthday;
            this.lueNational.Text = this.nationalManager.GetNationalById(Employee.NationalId).Name;
            this.lueReligious.Text = this.religiousManager.GetReligiousById(Employee.ReligiousId).Name;
            this.lueDepartmental.Text = this.departmentalManager.GetDepartmentalById(Employee.DepartmentalId).Name;
        }
        #endregion

        #region Init form
        public frmEmployeeEdit()
        {
            InitializeComponent();
            this.employeeManager = new EmployeeManager();
        }

        private void frmAuthorsAdd_Load(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
            this.SetMaxLengthOfTextBoxes();
            this.LoadDepartmental();
            this.LoadNational();
            this.LoadReligious();
            this.LoadData();
           
        }
        #endregion

        #region Validate

        private void txtName_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtFullName.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepName.ClearErrors();
            }
        }


        private void txtCode_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
               //
            }
            else
            {
                if (this.employeeManager.HasCodeDuplicated(this.txtCode.Text.Trim(),Employee.Id))
                {
                    this.dxepCode.SetError(this.txtCode, "Mã nhân viên " + this.txtCode.Text.Trim() + " đã tồn tại trong hệ thống. Hãy chọn một mã nhân viên khác.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtCode.Focus();
                    return;
                }

                this.dxepCode.ClearErrors();
            }
        }


        private void txtMobile_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtMobile.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                if (this.employeeManager.HasMobileDuplicated(this.txtMobile.Text.Trim(),Employee.Id))
                {
                    this.dxepMobile.SetError(this.txtMobile, "Số điện thoại nhân viên " + this.txtEmail.Text.Trim() + " đã tồn tại trong hệ thống. Vui lòng kiểm tra lại.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtMobile.Focus();
                    return;
                }

                this.dxepMobile.ClearErrors();
            }
        }

        private void txtEmail_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtEmail.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                if (this.employeeManager.HasEmailDuplicated(this.txtEmail.Text.Trim(),Employee.Id))
                {
                    this.dxepEmail.SetError(this.txtEmail, "Email nhân viên " + this.txtEmail.Text.Trim() + " đã tồn tại trong hệ thống. Vui lòng kiểm tra lại.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtEmail.Focus();
                    return;
                }

                this.dxepEmail.ClearErrors();
            }
        }

        private void txtAddress_EditValueChanged(object sender, EventArgs e)
        {
            if (txtAddress.Text.Trim() != String.Empty)
            {
                this.dxepAddress.ClearErrors();
            }
        }

        private void cbSex_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbSex.Text.Trim() != String.Empty)
            {
                this.dxepSex.ClearErrors();
            }
        }

        private void txtIdentityCard_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtIdentityCard.Text.Trim() != String.Empty)
            {
                this.dxepIdentityCard.ClearErrors();
            }
        }
        private void lueNational_EditValueChanged(object sender, EventArgs e)
        {
            if (this.lueNational.Text.Trim() != String.Empty)
            {
                this.dxepNational.ClearErrors();
            }
        }
        private void lueReligious_EditValueChanged(object sender, EventArgs e)
        {
            if (this.lueReligious.Text.Trim() != String.Empty)
            {
                this.dxepReligious.ClearErrors();
            }
        }
        private void lueDepartmental_EditValueChanged(object sender, EventArgs e)
        {
            if (this.lueDepartmental.Text.Trim() != String.Empty)
            {
                this.dxepDepartmental.ClearErrors();
            }
        }
        #endregion

        #region Button_click
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            var employee = new Employee();


            //validation name
            if (this.txtFullName.Text.Trim().Length == 0)
            {
                this.dxepName.SetError(this.txtFullName, "Tên nhân viên không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtFullName.Focus();
                return;
            }
            else
            {
                this.dxepName.ClearErrors();
            }
            //end validation name


            //validation Code
            if (this.txtCode.Text.Trim().Length == 0)
            {
                this.dxepCode.SetError(this.txtCode, "Mã nhân viên không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtCode.Focus();
                return;
            }
            else if (this.employeeManager.HasCodeDuplicated(this.txtCode.Text.Trim(),Employee.Id) == true)
            {
                this.dxepName.SetError(this.txtCode, "Mã nhân viên " + this.txtCode.Text.Trim() + " đã tồn tại trong hệ thống. Hãy chọn một mã nhân viên khác.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtFullName.Focus();
                return;
            }
            else
            {
                this.dxepCode.ClearErrors();
            }
            //end validation Code

            //validation name
            if (this.txtAddress.Text.Trim().Length == 0)
            {
                this.dxepAddress.SetError(this.txtAddress, "Địa chỉ không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtAddress.Focus();
                return;
            }
            else
            {
                this.dxepAddress.ClearErrors();
            }
            //end validation name

            //validation name
            if (this.cbSex.Text.Trim().Length == 0)
            {
                this.dxepSex.SetError(this.cbSex, "Giới tính viên không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.cbSex.Focus();
                return;
            }
            else
            {
                this.dxepSex.ClearErrors();
            }
            //end validation name

            //validation name
            if (this.txtIdentityCard.Text.Trim().Length == 0)
            {
                this.dxepIdentityCard.SetError(this.txtIdentityCard, "Số CMND không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtIdentityCard.Focus();
                return;
            }
            else
            {
                this.dxepIdentityCard.ClearErrors();
            }
            //end validation name

            //validation name
            if (this.lueNational.Text.Trim().Length == 0)
            {
                this.dxepNational.SetError(this.lueNational, "Dân tộc không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.lueNational.Focus();
                return;
            }
            else
            {
                this.dxepNational.ClearErrors();
            }
            //end validation name

            //validation name
            if (this.lueReligious.Text.Trim().Length == 0)
            {
                this.dxepReligious.SetError(this.lueReligious, "Tôn giáo không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.lueReligious.Focus();
                return;
            }
            else
            {
                this.dxepReligious.ClearErrors();
            }
            //end validation name

            //validation name
            if (this.lueDepartmental.Text.Trim().Length == 0)
            {
                this.dxepDepartmental.SetError(this.lueDepartmental, "Phòng ban không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.lueDepartmental.Focus();
                return;
            }
            else
            {
                this.dxepDepartmental.ClearErrors();
            }
            //end validation name

            // End of validation
            
            /*****************************************************************/
            // Save into database                   
            employee.Id = Employee.Id;
            employee.Code = this.txtCode.Text.Trim();
            employee.Fullname = this.txtFullName.Text.Trim();
            employee.Mobile = this.txtMobile.Text.Trim();
            employee.Address = this.txtAddress.Text.Trim();
            employee.Email = this.txtEmail.Text.Trim();
            employee.Description = this.txtDescription.Text.Trim();
            employee.Status = 1;
            employee.Sex = this.cbSex.Text.Trim();
            employee.Birthday = this.dteBirthday.DateTime;
            employee.IdentityCard = this.txtIdentityCard.Text.Trim();
            employee.NationalId = (int)this.lueNational.EditValue;
            employee.DepartmentalId = (int)this.lueDepartmental.EditValue;
            employee.ReligiousId = (int)this.lueReligious.EditValue;
            employee.Description = this.txtDescription.Text.Trim();

            this.employeeManager.UpdateEmployee(employee);
            XtraMessageBox.Show("Dữ liệu sửa thành công.");
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.Cancel;
        }

        #endregion

        

        

        

     
       

       
  
      

 

      

     
        


    }
}
